package com.neolix.watch.util;

import android.content.SharedPreferences;


import com.google.gson.reflect.TypeToken;
import com.neolix.watch.domain.BaiDuWeatherResult;
import com.neolix.watch.domain.WeatherLocation;

import static android.content.Context.MODE_PRIVATE;
import static android.content.SharedPreferences.Editor;
import static com.neolix.watch.NeolixApplication.getInstance;
import static com.neolix.watch.util.Constant.CURRENT_LOCATION;
import static com.neolix.watch.util.Constant.FIRST_START_APP;
import static com.neolix.watch.util.Constant.SETTINGS;
import static com.neolix.watch.util.JsonConverter.convertObjectToJson;

public class SharedPreferencesUtil {

    public static final String OLD_WEATHER_INFO = "old_weather_info";
    public static final String LAST_DEVICE_ADDRESS = "last_device_address";
    public static final String IS_SEND_IN_COMING_INFO = "is_send_in_coming_info";
    public static final String IS_SEND_SMS_INFO = "is_send_sms_info";
    public static final String IS_SEND_WX_INFO = "is_send_wx_info";
    public static final String IS_SLEEP_MODE = "is_sleep_mode";
    public static final String GOAL_STEPS = "goal_steps";
    public static final String LOSE_DISTANCE = "lose_distance";
    public static final String CURRENT_STEPS = "current_steps";

    public static void firstStartApp(boolean isFirstStartApp) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putBoolean(FIRST_START_APP, isFirstStartApp);
        edit.commit();
    }

    public static boolean isFirstStartApp() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getBoolean(FIRST_START_APP, true);
    }

    public static void saveLocation(WeatherLocation boss) {
        SharedPreferences preference = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preference.edit();
        String user = convertObjectToJson(boss);
        edit.putString(CURRENT_LOCATION, user);
        edit.commit();
    }

    public static WeatherLocation getCurrentLocation() {
        SharedPreferences preference = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        String userInfoStr = preference.getString(CURRENT_LOCATION, "");
        if (StringUtil.isBlank(userInfoStr)) return null;
        return JsonConverter.convertJsonToObject(userInfoStr, TypeToken.get(WeatherLocation.class));
    }

    public static void saveWeatherInfo(BaiDuWeatherResult boss) {
        SharedPreferences preference = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preference.edit();
        String user = convertObjectToJson(boss);
        edit.putString(OLD_WEATHER_INFO, user);
        edit.commit();
    }

    public static BaiDuWeatherResult getWeatherInfo() {
        SharedPreferences preference = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        String userInfoStr = preference.getString(OLD_WEATHER_INFO, "");
        if (StringUtil.isBlank(userInfoStr)) return null;
        return JsonConverter.convertJsonToObject(userInfoStr, TypeToken.get(BaiDuWeatherResult.class));
    }

    public static void saveLastBluetoothDeviceAddress(String address) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putString(LAST_DEVICE_ADDRESS, address);
        edit.commit();
    }

    public static String getLastBluetoothDeviceAddress() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getString(LAST_DEVICE_ADDRESS, "");
    }

    public static void setIsSendInComingInfo(boolean checkState) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putBoolean(IS_SEND_IN_COMING_INFO, checkState);
        edit.commit();
    }

    public static void setIsSendSMSInfo(boolean checkState) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putBoolean(IS_SEND_SMS_INFO, checkState);
        edit.commit();
    }

    public static void setIsSendWXInfo(boolean checkState) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putBoolean(IS_SEND_WX_INFO, checkState);
        edit.commit();
    }

    public static void setSleepMode(boolean checkState) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putBoolean(IS_SLEEP_MODE, checkState);
        edit.commit();
    }

    public static boolean isSendInComingInfo() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getBoolean(IS_SEND_IN_COMING_INFO, true);
    }

    public static boolean isSendSMSInfo() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getBoolean(IS_SEND_SMS_INFO, true);
    }

    public static boolean isSendWXInfo() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getBoolean(IS_SEND_WX_INFO, false);
    }

    public static boolean isSleepMode() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getBoolean(IS_SLEEP_MODE, false);
    }

    public static int getGoalSteps() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getInt(GOAL_STEPS, 0);
    }

    public static int getLoseDistance() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getInt(LOSE_DISTANCE, 0);
    }

    public static void setGoalSteps(int steps) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putInt(GOAL_STEPS, steps);
        edit.commit();
    }

    public static void setLoseDistance(int steps) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putInt(LOSE_DISTANCE, steps);
        edit.commit();
    }

    public static void setCurrentSteps(int steps) {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        Editor edit = preferences.edit();
        edit.putInt(CURRENT_STEPS, steps);
        edit.commit();
    }

    public static int getCurrentSteps() {
        SharedPreferences preferences = getInstance().getSharedPreferences(SETTINGS, MODE_PRIVATE);
        return preferences.getInt(CURRENT_STEPS, 0);
    }
}